package utilities;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import dataFromXml.FileFilter;

public class UniqueFilterHandler {
	public static void UniqueFilterMRFile(String tFolder, String frameFolder) {

		// judge whether the template is here.
		String filePath = tFolder + File.separator + "MR_UniqueFilter.java";
		File tarFile = new File(filePath);
		if (tarFile.exists()) {
			return;
		}

		// if there is no target file, create it.
		BufferedWriter bw, bw_import;
		BufferedReader br;
		try {
			bw = new BufferedWriter(new FileWriter(filePath));
			bw_import = new BufferedWriter(new FileWriter(tFolder + File.separator + "tempImport", true));
			br = new BufferedReader(new FileReader(frameFolder + "UniqueFilterT"));

			String line;
			String[] strArray;

			while ((line = br.readLine()) != null) {
				strArray = line.split("#");
				if (strArray.length > 1) {
					String[] pathFolder = tFolder.split("/");
					strArray[1] = pathFolder[pathFolder.length - 1];
					line = strArray[0] + strArray[1] + strArray[2];
				}
				bw.write(line + "\n");
				bw.flush();
			} // while
			bw.write("\n");
			bw.flush();

			strArray = tFolder.split("/");
			bw_import.write("import " + strArray[strArray.length-1] + ".MR_UniqueFilter.UniqueFilterMapper;\n");
			bw_import.write("import " + strArray[strArray.length-1] + ".MR_UniqueFilter.UniqueFilterReducer;\n");
			bw_import.flush();
			
			bw.close();
			br.close();
			bw_import.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			bw = null;
			br = null;
		}
	}

	public static void UniqueFilterProfile(String tFolder, String frameFolder, FileFilter ffNode,
			String uniqueAttr, int jobNum, int count) {

		BufferedWriter bw;
		BufferedReader br;
		try {
			bw = new BufferedWriter(new FileWriter(tFolder + File.separator + "tempProfile", true));
			br = new BufferedReader(new FileReader(frameFolder + "UniqueFilterP"));
			
			String line;

			while ((line = br.readLine()) != null) {
				
				String[] strArray = line.split("#");
				if (strArray[1].equals("0")) {
					strArray[1] = ffNode.getAttrNames();
				} else if (strArray[1].equals("1")) {
					strArray[1] = uniqueAttr;
				} else if (strArray[1].equals("2")) {
					strArray[1] = jobNum + "";
					if (strArray.length > 5 && strArray[3].equals("3")) {
						strArray[3] = ffNode.getTableName();
						if (strArray[5].equals("4")) {
							strArray[5] = (count - 1) + "";
						} else if (strArray[5].equals("5")) {
							strArray[5] = count + "";
						}
					}
				} else {
					assert false : "Unique filter profile may be error!";
				}
				
				line = "";
				for (int i = 0; i < strArray.length; i++) {
					line += strArray[i];
				}
				bw.write(line + "\n");
				bw.flush();
			}
			bw.write("\n");
			bw.flush();

			bw.close();
			br.close();
			System.out.println("Unique Filter profile is OK!");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
